﻿/// <reference path="../Content/ngViews/ngHome.html" />
var App = angular.module("App", ["ngRoute"]);
App.config(function ($routeProvider) {
    $routeProvider.when("/", {
        templateUrl: "../Content/ngViews/ngHome.html",
        controller: "ngHomeController"
    }).when("/CollegeDetail/:id", {
        templateUrl: "../Content/ngViews/ngCollegeDetail.html",
        controller: "ngCollegeDetailController"
    }).when("/AddNewCollege", {
        templateUrl: "../Content/ngViews/ngAddCollege.html",
        controller: "ngAddCollegeController"
    }).when("/EditCollege/:id", {
        templateUrl: "../Content/ngViews/ngEditCollege.html",
        controller: "ngEditCollegeController"
    }).when("/AddReview/:id", {
        templateUrl: "../Content/ngViews/ngAddReview.html",
        controller: "ngAddReviewController"
    })
})

App.controller("ngAddCollegeController", function ($scope, $http, $location) {
    $scope.colleges = [];
    $scope.AddNewCollege = function (college) {
        $http.post("api/Colleges/", college).success(function (data) {
            college.id = data;
            $scope.colleges.push({ name: college.name, address: college.address, phone: college.phone, description: college.description, tuition: college.tuition, acceptancerate: college.acceptancerate, image: college.image, url: college.url });
            college.name = "";
            college.address = "";
            college.phone = "";
            college.description = "";
            college.tuition = "";
            college.acceptancerate = "";
            college.enrollment = "";
            college.image = "";
            college.url = "";
            $location.path("CollegeDetail/" + college.id);
        }).error(function () {
            console.log(JSON.stringify(data));
        });
    };
});

App.controller("ngHomeController", function ($scope, $http, $location) {
    $scope.colleges = [];
    var avgrating = function () {
        for (var i in $scope.colleges) {
            var sum = 0;
            var count = 0;
            for (var j in $scope.colleges[i].ReviewList) {
                sum += $scope.colleges[i].ReviewList[j].Rating;
                count++;
            }
            $scope.colleges[i].AverageRating = (sum / count);
        }
    }

    $http.get("api/Colleges/").success(function (data) {
        for (var x in data) {
            data[x].id = x;
            $scope.colleges.push(data[x]);
        }
        avgrating();
    }).error(function (data) {
        console.log(JSON.stringify(data));
    });

    $scope.reviews = [];

    $scope.AddNewCollege = function () {
        $location.path("AddNewCollege/");
    }

    $scope.CollegeDetail = function (id) {
        $location.path("CollegeDetail/" + id);
    }
});

App.controller("ngCollegeDetailController", function ($location, $http, $scope, $routeParams) {


        $scope.college;
    var getcolleges = function () {
        $http.get("api/Colleges/" + $routeParams.id).success(function (data) {
            $scope.college = data;
        }).error(function (data) {
            console.log(JSON.stringify(data));
        });
    }
        $scope.reviews = [];
    var getreviews = function () {
        $http.get("api/Reviews/" + $routeParams.id).success(function (data) {
            for (var x in data) {
                data[x].id = x;
                console.log(data[x]);
                $scope.reviews.push(data[x]);
            }
        }).error(function (data) {
            console.log(JSON.stringify(data));
        });
    }
    var getavgrating = function () {
        var sum = 0;
        var count = 0;
        for (var j in $scope.reviews) {
            sum += $scope.reviews.Rating;
            count++;
        }
        $scope.college.AverageRating = (sum / count);
        console.log(sum / count);
    }
    getcolleges();
    getreviews();
    getavgrating();
    $scope.EditCollege = function () {
        //alert("go to edit college page");
        $location.path("EditCollege/" + $scope.college.Id);
    };
    $scope.DeleteCollege = function () {
        //need to edit the college array on client side
        $http.delete("api/Colleges/" + $scope.college.Id).success(function () { getcolleges(); $location.path("/"); });
    };
    $scope.AddReview = function () {
        //alert("go to add review page")
        $location.path("AddReview/" + $scope.college.Id);
    }
    $scope.DeleteReview = function (id) {
        //need success function
        $http.delete("api/Reviews/" + id).success(function () { getreviews() });
        //$location.path("/CollegeDetail/" + $scope.college.Id);
    }
});

App.controller("ngEditCollegeController", function ($scope, $http, $routeParams, $location) {
    $scope.college;
    $http.get("api/Colleges/" + $routeParams.id).success(function (data) {
        $scope.college = data;
    }).error(function (data) {
        console.log(JSON.stringify(data));
    });

    $scope.SaveChanges = function (college) {
        $http.put("api/Colleges/", college).success(function (data) {
            console.log(data);
            college = data;
            $location.path("CollegeDetail/" + college.Id);
        }).error(function () {
            console.log(JSON.stringify(data));
        });
    }
});

App.controller("ngAddReviewController", function ($scope, $http, $routeParams, $location) {
    $scope.college;
    $http.get("api/Colleges/" + $routeParams.id).success(function (data) {
        $scope.college = data;
        //$scope.college.getAvgRating();
    }).error(function (data) {
        console.log(JSON.stringify(data));
    });
    $scope.reviews = [];
    $scope.AddNewReview = function (review) {
        review.CollegeId = $routeParams.id;
        $http.post("api/Reviews/", review).success(function (data) {
            review.id = data;
            $scope.reviews.push({ Name: review.Name, Comment: review.Comment, Rating: review.Rating });
            review.Name = "";
            review.Comment = "";
            //$scope.college.getAvgRating();
            $location.path("CollegeDetail/" + review.CollegeId);
        }).error(function () {
            console.log(JSON.stringify(data));
        });
    }
    //$scope.college.getAvgRating = function () {
    //    var total;
    //    var count = 0;
    //    for (var i in $scope.reviews) {
    //        total += $scope.reviews.Rating;
    //        count++;
    //    }
    //    alert(total / count);
    //    college.avgRating = (total / count);
    //}
});